package cn.change.org.controller;
import cn.change.basic.exception.BusinssException;
import cn.change.basic.util.AjaxResult;
import cn.change.basic.util.ExcelUtil;
import cn.change.basic.util.PageList;
import cn.change.basic.vo.ShopVo;
import cn.change.org.domain.Shop;
import cn.change.org.domain.ShopAuditLog;
import cn.change.org.query.DepartmentQuery;
import cn.change.org.service.ShopService;
import org.apache.commons.lang3.mutable.MutableInt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.Multipart;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public ShopService shopService;

    /**
     * 导出
     * @param response
     */
    @GetMapping("/export/excel")
    public void  exportExcel(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    /**
     * 导入
     * @param file
     */
    @GetMapping("/import/excel")
    public void  importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file,1,1,Shop.class);
        System.out.println("共导入"+ shops.size()+"条数据");
    }

    /**
     * 每种状态对应的店铺数量list
     * @return
     */
    @GetMapping("/echars")
    public Map<String,Object> shopEchars(){
        List<ShopVo> shopVos = shopService.loadStateStatistics();
        List<Integer> states = new ArrayList<>();
        List<Long> nums = new ArrayList<>();
        for (ShopVo shopVo : shopVos) {
            states.add(shopVo.getState());
            nums.add(shopVo.getNum());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }
    /*审核通过*/
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.me();
        }catch (BusinssException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }

    }

    /*审核驳回*/
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return  AjaxResult.me();
        }catch (BusinssException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinssException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }

    }
    /**
     *增加或修改
     * @param
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() != null){
                shopService.update(shop);
            }
            shopService.add(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
    /**
     * 查询单条数据
     *
     * @param id
     * @return
     * @ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”,
     * response = “接口返回参数类型”, notes = “接口发布说明”；
     */
    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping()
    public List<Shop> list(){
        return shopService.queryAll();
    }

    /**
     * 高级查询和分页
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    public PageList<Shop> loadPage(@RequestBody DepartmentQuery query) {
        return shopService.queryPage(query);
    }
}
